package at.fhv.gis.service;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import at.fhv.gis.db.DbProvider;

@Controller
@RequestMapping("/gis")
public class GisService {

	@RequestMapping(value = "/points", produces = { "application/json" }, method = RequestMethod.GET)
	public @ResponseBody
	String getAllPoints() {
		try {
			return DbProvider.getInstance().getAllPoints();
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@RequestMapping(value = "/polygons", produces = { "application/json" }, method = RequestMethod.GET)
	public @ResponseBody
	String getAllPolygons() {
		try {
			return DbProvider.getInstance().getAllPolygons();
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@RequestMapping(value = "/lines", produces = { "application/json" }, method = RequestMethod.GET)
	public @ResponseBody
	String getAllLines() {
		try {
			return DbProvider.getInstance().getAllLines();
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@RequestMapping(value = "/bbox", produces = { "application/json" }, method = RequestMethod.GET)
	public @ResponseBody
	String getFromBoundingBox(String bbox) {
		try {
			String[] splitted = bbox.split(",");
			double long1 = Double.parseDouble(splitted[0]);
			double lat1 = Double.parseDouble(splitted[1]);
			double long2 = Double.parseDouble(splitted[2]);
			double lat2 = Double.parseDouble(splitted[3]);

			return DbProvider.getInstance().getFromBoundingBox(long1, lat1,
					long2, lat2);
		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

}
